#include "detectionwindowinfo.h"

DetectionWindowInfo::DetectionWindowInfo() {

}

DetectionWindowInfo& DetectionWindowInfo::operator= (DetectionWindowInfo& detectionWindowInfo){
    m_nRow = detectionWindowInfo.GetImageRow();
    m_nColumn = detectionWindowInfo.GetImageCol();
    m_nTotal = detectionWindowInfo.GetImageTotal();

    RemoveSeatFace(-1);

    QVector<SubImageInfo*> subImageInfos = detectionWindowInfo.GetSeatFaces();
    for(int i = 0; i < subImageInfos.count();i++){
        SubImageInfo* pSubImageInfo_ = subImageInfos.at(i);
        SubImageInfo* pSubImageInfo = new SubImageInfo();
        (*pSubImageInfo) = (*pSubImageInfo_);

        AppendSeatFace(pSubImageInfo);
    }
    return *this;
}

void DetectionWindowInfo::ResizeSeatFaces(int nCount){
    if(nCount > m_vecSeatFaces.count()){
        for(int i = m_vecSeatFaces.count(); i < nCount; i++){
            SubImageInfo* psf = new SubImageInfo();
            m_vecSeatFaces.append(psf);
        }
    }else if(nCount < m_vecSeatFaces.count()){
        for(int i = m_vecSeatFaces.count() - 1; i > nCount - 1 ; i--){
            SubImageInfo* psf = m_vecSeatFaces.at(i);
            delete psf;
            m_vecSeatFaces.removeLast();
        }
    }
}

void DetectionWindowInfo::RemoveSeatFace(int index){
    int nTotal = m_vecSeatFaces.count();
    if(index == -1){
        for(int i = nTotal - 1;i >= 0;i--){
            SubImageInfo* psf = m_vecSeatFaces.at(i);
            delete psf;
            m_vecSeatFaces.remove(i);
        }
    }else{
        SubImageInfo* psf = m_vecSeatFaces.at(index);
        delete psf;
        m_vecSeatFaces.remove(index);
    }
}

void DetectionWindowInfo::AppendSeatFace(SubImageInfo* pSeatFace){
    m_vecSeatFaces.append(pSeatFace);
}

void DetectionWindowInfo::SetSeatFaceContent(int index,QString strFace,QString strCamera){
    SubImageInfo* psf = m_vecSeatFaces.at(index);
    psf->SetPosition(index);
    psf->SetFace(strFace);
    psf->SetCameraName(strCamera);
}

bool DetectionWindowInfo::WriteXML(QXmlStreamWriter &writer){
    writer.writeStartElement("DetectionWindowInfo");
    writer.writeAttribute("Column",QString::number(m_nColumn));
    writer.writeAttribute("Row",QString::number(m_nRow));
    writer.writeAttribute("Total",QString::number(m_nTotal));

    for(int i = 0;i < m_vecSeatFaces.count();i++){
        SubImageInfo* psf = m_vecSeatFaces.at(i);
        writer.writeStartElement("SeatFace");
        writer.writeTextElement("Face",psf->GetFace());
        writer.writeTextElement("Camera",psf->GetCameraName());
        writer.writeTextElement("Position",QString::number(psf->GetPosition()));
        writer.writeEndElement();
    }

    writer.writeEndElement();
    return true;
}

void DetectionWindowInfo::ParseFacePosition_(QXmlStreamReader &reader,QString &strFace,QString &strCamera,QString &strPosition){
    while (!reader.atEnd()) {
        reader.readNext();

        if (reader.isStartElement()) {  // 开始元素
            QString strElementName = reader.name().toString();
            if (QString::compare(strElementName, "Face") == 0) {
                strFace = reader.readElementText();
            }

            if (QString::compare(strElementName, "Camera") == 0) {
                strCamera = reader.readElementText();
            }

            if (QString::compare(strElementName, "Position") == 0) {
                strPosition = reader.readElementText();
            }
        } else if (reader.isEndElement()) {  // 结束元素
            QString strElementName = reader.name().toString();
            if (QString::compare(strElementName, "SeatFace") == 0) {
                break;  // 跳出循环（解析 QXmlStreamReader::EndDocument）
            }
        }
    }
}

void DetectionWindowInfo::ParseSeatFace(QXmlStreamReader &reader){
    m_vecSeatFaces.clear();

    while (!reader.atEnd()) {
        reader.readNext();

        if (reader.isStartElement()) {  // 开始元素
            QString strElementName = reader.name().toString();
            if (QString::compare(strElementName, "SeatFace") == 0) {
                SubImageInfo* pSubImageInfo = new SubImageInfo();
                QString strFace,strCamera,strPosition;
                ParseFacePosition_(reader,strFace,strCamera,strPosition);
                pSubImageInfo->SetFace(strFace);
                pSubImageInfo->SetCameraName(strCamera);
                pSubImageInfo->SetPosition(strPosition.toInt());
                m_vecSeatFaces.append(pSubImageInfo);
            }
        } else if (reader.isEndElement()) {  // 结束元素
            QString strElementName = reader.name().toString();
            if (QString::compare(strElementName, "DetectionWindowInfo") == 0) {
                break;  // 跳出循环（解析 QXmlStreamReader::EndDocument）
            }
        }
    }
}

bool DetectionWindowInfo::ReadXML(QXmlStreamReader &reader){
    QString className = reader.name().toString();

    QXmlStreamAttributes attributes = reader.attributes();
    if(attributes.hasAttribute("Column")) {
        m_nColumn = attributes.value("Column").toString().toInt();
    }

    if(attributes.hasAttribute("Row")) {
        m_nRow = attributes.value("Row").toString().toInt();
    }

    if(attributes.hasAttribute("Total")) {
        m_nTotal = attributes.value("Total").toString().toInt();
    }

    while (!(reader.tokenType() == QXmlStreamReader::EndElement &&
             reader.name() == QStringLiteral("DetectionWindowInfo"))) {

        className = reader.name().toString();

        {
            ParseSeatFace(reader);
        }
        //reader.readNext();
    }

    //reader.readNext();
    //reader.readNext();
    return true;
}
